package com.flink.streaming.web.controller.api;


import com.alibaba.fastjson.JSONObject;
import com.flink.streaming.web.common.RestResult;
import com.flink.streaming.web.controller.web.BaseController;
import com.flink.streaming.web.exceptions.BizException;
import com.flink.streaming.web.model.entity.AzkanbanOfflineSQL;
import com.flink.streaming.web.model.param.AzKaBanRunParam;
import com.flink.streaming.web.service.AzKaBanSqlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/api/azKaBanSql")
public class AzKaBanSqlApiController extends BaseController {


    @Autowired
    private AzKaBanSqlService azKaBanSqlService;

    @ResponseBody
    @RequestMapping(value = "/submitSql", method = RequestMethod.POST)
    public RestResult submitSql(AzkanbanOfflineSQL offlineSQL) {
        try {
            azKaBanSqlService.submitSql(offlineSQL, getUserName());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("提交失败 data={}", JSONObject.toJSONString(offlineSQL), e);
            return RestResult.error(e.getMessage());
        }

        return RestResult.success();
    }



    @ResponseBody
    @RequestMapping(value = "/editSql", method = RequestMethod.POST)
    public RestResult editSql(AzkanbanOfflineSQL offlineSQL) {
        try {
            azKaBanSqlService.editSql(offlineSQL, getUserName());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("提交失败 data={}", JSONObject.toJSONString(offlineSQL), e);
            return RestResult.error(e.getMessage());
        }

        return RestResult.success();
    }


    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public RestResult updateStatus(Long id) {
        try {
            azKaBanSqlService.updateStatus(id, getUserName());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("提交失败 data={}", id, e);
            return RestResult.error(e.getMessage());
        }

        return RestResult.success();
    }

    @ResponseBody
    @RequestMapping(value = "/runSql", method = RequestMethod.POST)
    public RestResult<List<String>> runSql(AzKaBanRunParam param){

        List<String> list;
        try {
            checkRunSQL(param);
            list = azKaBanSqlService.runSql(param);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("提交失败 data={}", param, e);
            return RestResult.error(e.getMessage());
        }

        return RestResult.success(list);
    }

    private void checkRunSQL(AzKaBanRunParam param) {
        if (StringUtils.isEmpty(param.getIndicatorCycle())){
            throw new BizException("周期参数必传");
        }

    }


}
